﻿#light

/// File: RegressionTests\LightweightControls.fs
/// 
/// Author: Giacomo Righetti (righetti@di.unipi.it)
///   
/// ------------------------------------------------------------
/// Copyright (c) Microsoft Corporation.  All rights reserved.
/// 
/// The use and distribution terms for this software are 
/// contained in the file named license.txt, which can be found 
/// in the root of this distribution.
/// By using this software in any fashion, you are agreeing to 
/// be bound by the terms of this license.
///
/// You must not remove this notice, or any other, from this
/// software.
/// ------------------------------------------------------------

#r "ViewletControls.dll"
#r "VSLabFSICore.dll"

open System
open System.Drawing
open System.Windows.Forms

open VSLab
open LightweightControls

type ShowStringViewlet() =
  inherit Viewlet()
  
  [<DefaultValue>]
  val mutable displayString : string
    
  override self.OnPaint(e) =
    base.OnPaint(e)
    let g = e.Graphics
    g.DrawString(self.displayString, self.Font, Brushes.Black, 0.0f, 0.0f)

type LightweightControlsViewlet() as x =
  inherit ShowStringViewlet()

  let flatBtn = LightweightButton(Position = Point(30,155), Text = "FlatStyle",  FlatStyle = FlatStyle.Flat)
      
  let setupFlatBtn = 
    flatBtn.FlatAppearance.BorderColor <- Color.Black
    flatBtn.FlatAppearance.MouseOverBackColor <- Color.Green
    flatBtn.FlatAppearance.MouseDownBackColor <- Color.LightGreen
    flatBtn.Click.Add(fun _ ->
      flatBtn.FlatAppearance.BorderSize <- 2; 
      flatBtn.FlatAppearance.BorderColor <- Color.DarkOliveGreen)
                    
  do 
    x.displayString <- "This Viewlet shows the Lightweight controls"
    x.SuspendUpdating()
    x.AddRange([
                 LightweightButton(Position = Point(30,50),  Text = "OldStyle",   FlatStyle = FlatStyle.OldStyle);
                 LightweightButton(Position = Point(30,90),  Text = "LWButton2",  Size = Size(165,50));
                 flatBtn;
                 LightweightButton(Position = Point(120,50), Text = "PopupStyle", FlatStyle = FlatStyle.Popup);
                 LightweightButton(Position = Point(210,50), Text = "DisabledBtn", Enabled = false);
                 LightweightButton(Position = Point(320,50), Text = "DisabledOldStyleBtn", FlatStyle = FlatStyle.OldStyle, Enabled = false);
                 LightweightButton(Position = Point(210,90), Text = "DisabledFlatBtn", FlatStyle = FlatStyle.Flat, Enabled = false);
                 LightweightButton(Position = Point(320,90), Text = "DisabledPopupBtn", FlatStyle = FlatStyle.Popup, Enabled = false);
               ])
    x.AddRange([
                 LWHorizontalScrollbar(Position = Point(30,220), Size = Size(150,30));
                 LWHorizontalScrollbar(Position = Point(30,280), Size = Size(150,30), Enabled = false)
               ])
    x.AddRange([    
                 LWVerticalScrollbar(Position = Point(200,220), Size = Size(30,130));
                 LWVerticalScrollbar(Position = Point(250,220), Size = Size(30,130), Enabled = false)
               ])  
    x.Add(LightweightTrackBar(Position = Point(30,400), Size = Size(200,40)))
    x.Add(LightweightTrackBar(Position = Point(300,400), Size = Size(40,70), Orientation = Orientation.Horizontal))
    x.AddRange([
                 LightweightGroupBox(Position = Point(15,25),  Text = "LightweightButton",    Size = Size(470, 170));
                 LightweightGroupBox(Position = Point(15,200), Text = "LightweightScrollbar", Size = Size(470, 170));
                 LightweightGroupBox(Position = Point(15,375), Text = "LightweightTrackBar",  Size = Size(470, 110))
               ])
    x.ResumeUpdating(false)

let lwctrlv = new LightweightControlsViewlet()  
lwctrlv.Show()
lwctrlv.SetViewletStyle(ControlStyles.DoubleBuffer, true)
lwctrlv.Close()

type ZOrderViewlet() as x =
  inherit ShowStringViewlet()

  let btn1 = LightweightButton(Position = Point(30,40),   Size = Size(200,200), Text = "Button1")
  let btn2 = LightweightButton(Position = Point(80,130),  Size = Size(200,200), Text = "Button2")
  let btn3 = LightweightButton(Position = Point(150,180), Size = Size(200,200), Text = "Button3")
  let btn4 = LightweightButton(Position = Point(300,40), Size = Size(140,50), Text = "Bring btn2 to front")
  
  do 
    x.displayString <- "This Viewlet shows z-order manipulations of Lightweight controls"
    x.SuspendUpdating()
    x.Add(btn1)
    x.Add(btn2)
    x.Add(btn3)
    x.Add(btn4)
    x.ResumeUpdating(false)
    x.addClickHandler(btn1)
    x.addClickHandler(btn2)
    x.addClickHandler(btn3)
    // FIX: there is a bug .. 
    btn4.Click.Add(fun _ -> btn2.BringToFront())//; x.Invalidate())
    
  member private x.addClickHandler (btn : LightweightButton) = 
    btn.Click.Add(fun _ -> 
      btn.BringToFront()
      x.displayString <- "BringToFront: " + btn.Text
      x.Invalidate())
        
let zov = new ZOrderViewlet()  
zov.Show() 
zov.Close()

type LightweightPanelViewlet() as x =
  inherit ShowStringViewlet()
  
  do 
    x.displayString <- "This Viewlet shows different kinds of LightweightPanel"
    x.SuspendUpdating()
    x.AddRange([
                 LightweightPanel(BorderStyle = Border3DStyle.Bump);
                 LightweightPanel(BorderStyle = Border3DStyle.Etched);
                 LightweightPanel(BorderStyle = Border3DStyle.Flat);
                 LightweightPanel(BorderStyle = Border3DStyle.Raised);
                 LightweightPanel(BorderStyle = Border3DStyle.RaisedInner);
                 LightweightPanel(BorderStyle = Border3DStyle.RaisedOuter);
                 LightweightPanel(BorderStyle = Border3DStyle.Sunken);
                 LightweightPanel(BorderStyle = Border3DStyle.SunkenInner);
                 LightweightPanel(BorderStyle = Border3DStyle.SunkenOuter)
               ])
    x.setupPanels()
    x.ResumeUpdating(false)

  member private x.setupPanels() =
    let i = ref 1
    x.LightweightControls.ForEach(fun p -> 
        p.Size <- Size(90,90)
        p.Position <- Point (20 * !i, 20 * !i)
        i := !i + 2)

let lwpv = new LightweightPanelViewlet()
lwpv.Show()
lwpv.Close()

type TextAlignmentViewlet() as x =
  inherit ShowStringViewlet()
  
  do 
    x.displayString <- "This Viewlet tests text alignment capability"
    x.SuspendUpdating()
    x.AddRange([
                 LightweightButton(Position = Point(30,30),   Size = Size(100,100), Text = "button1", TextAlign = ContentAlignment.TopLeft);
                 LightweightButton(Position = Point(160,30),  Size = Size(100,100), Text = "button2", TextAlign = ContentAlignment.TopCenter);
                 LightweightButton(Position = Point(290,30),  Size = Size(100,100), Text = "button3", TextAlign = ContentAlignment.TopRight);
                 LightweightButton(Position = Point(30,160),  Size = Size(100,100), Text = "button4", TextAlign = ContentAlignment.MiddleLeft);
                 LightweightButton(Position = Point(160,160), Size = Size(100,100), Text = "button5", TextAlign = ContentAlignment.MiddleCenter);
                 LightweightButton(Position = Point(290,160), Size = Size(100,100), Text = "button6", TextAlign = ContentAlignment.MiddleRight);
                 LightweightButton(Position = Point(30,290),  Size = Size(100,100), Text = "button7", TextAlign = ContentAlignment.BottomLeft);
                 LightweightButton(Position = Point(160,290), Size = Size(100,100), Text = "button8", TextAlign = ContentAlignment.BottomCenter);
                 LightweightButton(Position = Point(290,290), Size = Size(100,100), Text = "button9", TextAlign = ContentAlignment.BottomRight)
               ])
    x.ResumeUpdating(false)
    
let tav = new TextAlignmentViewlet()
tav.Show()
tav.Close()

type ImageAlignmentViewlet() as x =
  inherit ShowStringViewlet()
  
  do 
    x.displayString <- "This Viewlet tests image alignment capability"
    let path = @"C:\Users\Giacomo\Desktop\vslab_repo\RegressionTests\image.bmp"
    let image = Image.FromFile(path)
    x.SuspendUpdating()
    x.AddRange([
                 LightweightButton(Position = Point(30,30),   Size = Size(100,100), Image = image, ImageAlign = ContentAlignment.TopLeft);
                 LightweightButton(Position = Point(160,30),  Size = Size(100,100), Image = image, ImageAlign = ContentAlignment.TopCenter);
                 LightweightButton(Position = Point(290,30),  Size = Size(100,100), Image = image, ImageAlign = ContentAlignment.TopRight);
                 LightweightButton(Position = Point(30,160),  Size = Size(100,100), Image = image, ImageAlign = ContentAlignment.MiddleLeft);
                 LightweightButton(Position = Point(160,160), Size = Size(100,100), Image = image, ImageAlign = ContentAlignment.MiddleCenter);
                 LightweightButton(Position = Point(290,160), Size = Size(100,100), Image = image, ImageAlign = ContentAlignment.MiddleRight);
                 LightweightButton(Position = Point(30,290),  Size = Size(100,100), Image = image, ImageAlign = ContentAlignment.BottomLeft);
                 LightweightButton(Position = Point(160,290), Size = Size(100,100), Image = image, ImageAlign = ContentAlignment.BottomCenter);
                 LightweightButton(Position = Point(290,290), Size = Size(100,100), Image = image, ImageAlign = ContentAlignment.BottomRight)
               ])
    x.ResumeUpdating(false)
    
let iav = new ImageAlignmentViewlet()
iav.Show()
iav.Close()

type AnchoringViewlet() as x =
  inherit ShowStringViewlet()
  
  do 
    x.displayString <- "This Viewlet tests anchoring capability"
    
let av = new AnchoringViewlet()
let btn = LightweightButton(Position = Point(50,50), Size = Size(100,100), Anchor = AnchorStyles.None)
av.Add(btn)
av.Show()

btn.Anchor <- AnchorStyles.Top ||| AnchorStyles.Left
btn.Anchor <- AnchorStyles.Left ||| AnchorStyles.Right
btn.Anchor <- AnchorStyles.Top ||| AnchorStyles.Left ||| AnchorStyles.Right ||| AnchorStyles.Bottom

av.Close()

type PictureBoxAndLabelViewlet() as x =
  inherit ShowStringViewlet()
  
  do 
    let path = @"C:\Users\Giacomo\Desktop\vslab_repo\RegressionTests\image.bmp"
    let image = Image.FromFile(path)
    x.displayString <- "This Viewlet tests picture box and label controls"
    x.SuspendUpdating()
        
    x.Add(LightweightLabel(Position = Point(20,30), Text = "AutoSize, FixedSingle"))
    x.Add(LightweightPictureBox(Position = Point(24,50), Size = Size(100,100), Image = image, SizeMode = PictureBoxSizeMode.AutoSize,
                                BorderStyle = BorderStyle.FixedSingle))
    
    x.Add(LightweightLabel(Position = Point(170,30), Text = "AutoSize, Fixed3D"))
    x.Add(LightweightPictureBox(Position = Point(174,50), Size = Size(100,100), Image = image, SizeMode = PictureBoxSizeMode.AutoSize,
                                BorderStyle = BorderStyle.Fixed3D))
                                
    x.Add(LightweightLabel(Position = Point(300,30), Text = "AutoSize, None"))
    x.Add(LightweightPictureBox(Position = Point(304,50), Size = Size(100,100), Image = image, SizeMode = PictureBoxSizeMode.AutoSize,
                                BorderStyle = BorderStyle.None))
                                
    x.Add(LightweightLabel(Position = Point(20,130), Text = "CenterImage, FixedSingle"))
    x.Add(LightweightPictureBox(Position = Point(24,150), Size = Size(70,70), Image = image, SizeMode = PictureBoxSizeMode.CenterImage,
                                BorderStyle = BorderStyle.FixedSingle))
                                
    x.Add(LightweightLabel(Position = Point(170,130), Text = "CenterImage, Fixed3D"))
    x.Add(LightweightPictureBox(Position = Point(174,150), Size = Size(70,70), Image = image, SizeMode = PictureBoxSizeMode.CenterImage,
                                BorderStyle = BorderStyle.Fixed3D))
    
    x.Add(LightweightLabel(Position = Point(300,130), Text = "CenterImage, None"))
    x.Add(LightweightPictureBox(Position = Point(304,150), Size = Size(70,70), Image = image, SizeMode = PictureBoxSizeMode.CenterImage,
                                BorderStyle = BorderStyle.None))
                                
    x.Add(LightweightLabel(Position = Point(20,240), Text = "Normal, FixedSingle"))
    x.Add(LightweightPictureBox(Position = Point(24,260), Size = Size(70,70), Image = image, SizeMode = PictureBoxSizeMode.Normal,
                                BorderStyle = BorderStyle.FixedSingle))

    x.Add(LightweightLabel(Position = Point(170,240), Text = "Normal, Fixed3D"))
    x.Add(LightweightPictureBox(Position = Point(174,260), Size = Size(70,70), Image = image, SizeMode = PictureBoxSizeMode.Normal,
                                BorderStyle = BorderStyle.Fixed3D))

    x.Add(LightweightLabel(Position = Point(300,240), Text = "Normal, None"))
    x.Add(LightweightPictureBox(Position = Point(304,260), Size = Size(70,70), Image = image, SizeMode = PictureBoxSizeMode.Normal,
                                BorderStyle = BorderStyle.None))

    x.Add(LightweightLabel(Position = Point(20,350), Text = "StretchImage, FixedSingle"))
    x.Add(LightweightPictureBox(Position = Point(24,370), Size = Size(70,70), Image = image, SizeMode = PictureBoxSizeMode.StretchImage,
                                BorderStyle = BorderStyle.FixedSingle))
    
    x.Add(LightweightLabel(Position = Point(170,350), Text = "StretchImage, Fixed3D"))
    x.Add(LightweightPictureBox(Position = Point(174,370), Size = Size(70,70), Image = image, SizeMode = PictureBoxSizeMode.StretchImage,
                                BorderStyle = BorderStyle.Fixed3D))
                                
    x.Add(LightweightLabel(Position = Point(300,350), Text = "StretchImage, None"))
    x.Add(LightweightPictureBox(Position = Point(304,370), Size = Size(70,70), Image = image, SizeMode = PictureBoxSizeMode.StretchImage,
                                BorderStyle = BorderStyle.None))
                                
    x.Add(LightweightLabel(Position = Point(20,460), Text = "Zoom, FixedSingle"))
    x.Add(LightweightPictureBox(Position = Point(24,480), Size = Size(100,100), Image = image, SizeMode = PictureBoxSizeMode.Zoom,
                                BorderStyle = BorderStyle.FixedSingle))
                                
    x.Add(LightweightLabel(Position = Point(170,460), Text = "Zoom, Fixed3D"))
    x.Add(LightweightPictureBox(Position = Point(174,480), Size = Size(100,100), Image = image, SizeMode = PictureBoxSizeMode.Zoom,
                                BorderStyle = BorderStyle.Fixed3D))

    x.Add(LightweightLabel(Position = Point(300,460), Text = "Zoom, None"))
    x.Add(LightweightPictureBox(Position = Point(304,480), Size = Size(100,100), Image = image, SizeMode = PictureBoxSizeMode.Zoom,
                                BorderStyle = BorderStyle.None))
                                
    x.ResumeUpdating(false)
   
let pblv = new PictureBoxAndLabelViewlet()
pblv.Show()
pblv.Close()

type TabIndexViewlet() as x =
  inherit ShowStringViewlet()
  
  let btn1 = LightweightButton(Position = Point(30,30), Size = Size(100,100))
  let btn2 = LightweightButton(Position = Point(150,150), Size = Size(100,100))
  let btn3 = LightweightButton(Position = Point(270,270), Size = Size(100,100))
  
  do 
    x.displayString <- "This Viewlet tests focus & TabIndex"
    x.SuspendUpdating()
    x.Add(btn1)
    btn1.Text <- btn1.TabIndex.ToString()
    x.Add(btn2)
    btn2.Text <- btn2.TabIndex.ToString()
    x.Add(btn3)
    btn3.Text <- btn3.TabIndex.ToString()
    x.ResumeUpdating(false)
    x.addFocusHandlers(btn1)
    x.addFocusHandlers(btn2)
    x.addFocusHandlers(btn3)
  
  member private x.addFocusHandlers (btn : LightweightButton) = 
    btn.LostFocus.Add(fun _ -> Console.WriteLine("btn-{0} lost focus", btn.Text))
    btn.GotFocus.Add(fun _ -> Console.WriteLine("btn-{0} got focus", btn.Text))

let tabv = new TabIndexViewlet()
tabv.Show()
tabv.Close()

type TextBoxViewlet() as x =
  inherit ShowStringViewlet()
  
  do
    x.displayString <- "This Viewlet shows the LightweightTextBox control"
    x.Add(LightweightTextBox(Position = Point(30,30), Size = Size(100,100)))
        
let tbv = new TextBoxViewlet()
tbv.Show()
tbv.Close()

type ToolTipViewlet() as x =
  inherit ShowStringViewlet()
  
  do
    x.displayString <- "This Viewlet shows the LightweightToolTip control"
    x.Add(LightweightToolTip(Text = "This is a ToolTip!"))
    
let ttv = new ToolTipViewlet()
ttv.Show()
ttv.Close()